package com.useeinfo.oa.modules.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.data.SelectVO;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.AuditFlow;
import com.useeinfo.oa.modules.base.enums.AuditTypeEnum;
import net.sf.json.JSONArray;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.*;

/**
 * @Auther: xudong.sun
 * @Date: 2018/12/7 15:46
 * @Description:
 */
@Repository("auditFlowDao")
public class AuditFlowDao implements CrudDao<AuditFlow> {

    private final static Logger logger = LoggerFactory.getLogger(AuditFlowDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    //根据部门id查询岗位List
    public int updateDefault(String moduleId,String auditType) {
        String sql = "UPDATE audit_flow set defaultv=false WHERE application_module_id="+moduleId+"  and audit_type='"+auditType+"' and delete_status=false";
        logger.info("拼接完成的sql语句为：{}",sql);
       int obj=entityManager.createNativeQuery(sql).executeUpdate();
       return obj;
    }


    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String flowName = queryHash.get("flowName");
        if (!StringUtils.isEmpty(flowName)) {
            conditionHash.put("(s.flowName = ?{paramIndex}) ", flowName);
        }
        String searchCode = queryHash.get("searchCode");
        if (!StringUtils.isEmpty(searchCode)) {
            conditionHash.put("(s.flowName like ?{paramIndex}) ", "%" + searchCode + "%");
        }
        String applicationName = queryHash.get("applicationName");
        if (!StringUtils.isEmpty(applicationName)) {
            conditionHash.put("(s.applicationModule.applicationName like ?{paramIndex}) ", "%" + applicationName + "%");
        }
        String auditType = queryHash.get("auditType");
        if (!StringUtils.isEmpty(auditType)) {
            conditionHash.put("(s.auditTypeEnum = ?{paramIndex}) ", AuditTypeEnum.valueOf(auditType));
        }
        String auditFlowName = queryHash.get("auditFlowName");
        if (!StringUtils.isEmpty(auditFlowName)) {
            conditionHash.put("(s.flowName = ?{paramIndex}) ", auditFlowName);
        }
        String level = queryHash.get("level");
        if (!StringUtils.isEmpty(level)) {
            conditionHash.put("(s.level = ?{paramIndex}) ", StringConverters.null2Int(level));
        }
        String enterpriseId = queryHash.get("enterpriseId");
        if (!StringUtils.isEmpty(enterpriseId)) {
            conditionHash.put("(s.enterprise.id = ?{paramIndex}) ", StringConverters.ToLong(enterpriseId));
        }
        String complete = queryHash.get("complete");
        if(StringUtils.isNotBlank(complete)){
            conditionHash.put("(s.auditTypeEnum = ?{paramIndex})", AuditTypeEnum.COMPLETE);
        }
        String delay = queryHash.get("delay");
        if(StringUtils.isNotBlank(delay)){
            conditionHash.put("(s.auditTypeEnum = ?{paramIndex})", AuditTypeEnum.DELAY);
        }
        String cancel = queryHash.get("cancel");
        if(StringUtils.isNotBlank(cancel)){
            conditionHash.put("(s.auditTypeEnum = ?{paramIndex})", AuditTypeEnum.CANCEL);
        }
        String type = queryHash.get("type");
        if(StringUtils.isNotBlank(type)){
            conditionHash.put("(s.auditTypeEnum = ?{paramIndex})", AuditTypeEnum.valueOf(type));
        }
        String isDefaultV = queryHash.get("isDefaultV");
        if(StringUtils.isNotBlank(isDefaultV)){
            conditionHash.put("(s.defaultV = ?{paramIndex} and s.flow is not null and s.flow !='')",StringConverters.null2Boolean(isDefaultV));
        }
        String applicationModule = queryHash.get("applicationModule");
        if(StringUtils.isNotBlank(applicationModule)){
            conditionHash.put("(s.applicationModule.applicationName like ?{paramIndex})", "%"+applicationModule+"%");
        }
        String remark = queryHash.get("remark");
        if(StringUtils.isNotBlank(remark)){
            conditionHash.put("(s.applicationModule.remark = ?{paramIndex})", remark);
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from AuditFlow s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<AuditFlow> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from AuditFlow s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<AuditFlow> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, AuditFlow.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public AuditFlow findModel(Long id) {
        return entityManager.find(AuditFlow.class, id);
    }

    @Override
    public Integer add(AuditFlow model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("AuditFlowDaoImpl添加auditFlowInfo成功！");
        return 1;
    }

    @Override
    public Integer update(AuditFlow model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        AuditFlow auditFlow = entityManager.find(AuditFlow.class, id);
        entityManager.remove(auditFlow);
        return 1;
    }

    @Override
    public Integer deleteLogic(AuditFlow model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
